package com.study.design.pattern.pattern.behavioral.observer.spring;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

/**
 * @Author: gongsl
 * @Date: 2025-03-23 23:49
 * @description: 事件发布者-股市
 */
@Component
public class StockMarket {
    private final ApplicationEventPublisher eventPublisher;
    private double currentStockPrice = 100.0; // 初始股价

    public StockMarket(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }

    public void setStockPrice(double newStockPrice) {
        double oldStockPrice = this.currentStockPrice;
        this.currentStockPrice = newStockPrice;
        System.out.println("StockMarket: 股价更新 -> 旧股价: $" + oldStockPrice + ", 新股价: $" + newStockPrice);
        eventPublisher.publishEvent(new StockChangeEvent(this, oldStockPrice, newStockPrice));
    }
}
